import time

from go.core.goboard import Draw
from go.core.gogame import Game
from go.core.gotypes import Battle, BoardSize
from go.core.strategy import human_move, bot_move_random, bot_move_mcts
from go.core.util import GameHelp

last_grid = []


# 落子和提子的画图逻辑方法
def draw_and_drop_point(_grid, _last_grid, _draw):
    for i in range(0, len(_grid)):
        if _grid[i].player == _last_grid[i].player:
            continue
        else:
            if (_grid[i].player is None) and (_last_grid[i].player is not None):
                _draw.drop_draw_point(_last_grid[i])
            elif (_grid[i].player is not None) and (_last_grid[i].player is not None) and \
                 (_grid[i].player != _last_grid[i].player):
                _draw.drop_draw_point(_last_grid[i])
                _draw.draw_point(_grid[i])
            elif(_grid[i].player is not None) and (_last_grid[i].player is None):
                _draw.draw_point(_grid[i])
        last_grid[i].row = _grid[i].row
        last_grid[i].col = _grid[i].col
        last_grid[i].player = _grid[i].player


# 测试方法
def test():
    battle = Battle.BOT_VS_BOT  # <1> 必填参数
    board_size = BoardSize.SMALL.value  # <2> 必填参数
    draw = Draw(board_size)
    draw.draw_board()
    game = Game(board_size)
    global last_grid
    last_grid = GameHelp.init_board_grid_points(board_size)
    cost_time = time.perf_counter()
    if battle == Battle.BOT_VS_BOT:
        while not game.is_game_over:
            bot_move_mcts(game)
            draw_and_drop_point(game.grid, last_grid, draw)
            time.sleep(1)  # <3> 由于bot_move运行速度较快，所以需要适当增加休眠时间。。。
    elif battle == Battle.BOT_VS_HUMAN:
        while not game.is_game_over:
            bot_move_random(game)
            draw_and_drop_point(game.grid, last_grid, draw)
            time.sleep(1)
            human_move(game)
            draw_and_drop_point(game.grid, last_grid, draw)
    elif battle == Battle.HUMAN_VS_BOT:
        while not game.is_game_over:
            human_move(game)
            draw_and_drop_point(game.grid, last_grid, draw)
            time.sleep(1)
            bot_move_random(game)
            draw_and_drop_point(game.grid, last_grid, draw)
    elif battle == Battle.HUMAN_VS_HUMAN:
        while not game.is_game_over:
            human_move(game)
            draw_and_drop_point(game.grid, last_grid, draw)
    else:
        raise Exception("对战类型错误")
    print(f'游戏结束，一共耗时:{time.perf_counter() - cost_time:.8f}秒，' + game.who_win().name + '赢')
    draw.draw_board(finish=True)


# 快速模拟mock_times次
def test_quick_mock(mock_times, board_size):
    for i in range(0, mock_times):
        game = Game(board_size)
        cost_time = time.perf_counter()
        game = GameHelp.quick_random_mock_game(game, None, True)
        print(f'游戏结束，一共耗时:{time.perf_counter() - cost_time:.8f}秒，' + game.who_win().name + '赢')


# 快速模拟一次画出最终棋局
def test_quick_mock_one_and_draw(board_size):
    game = Game(board_size)
    cost_time = time.perf_counter()
    game = GameHelp.quick_random_mock_game(game, None, True)
    print(f'游戏结束，一共耗时:{time.perf_counter() - cost_time:.8f}秒，' + game.who_win().name + '赢')
    draw = Draw(board_size)
    draw.draw_board()
    for p in game.grid:
        if p.player is not None:
            draw.draw_point(p)
    draw.draw_board(finish=True)


# 运行测试方法
test()
# test_quick_mock(100, 9)
# test_quick_mock_one_and_draw(9)
